package com.example.yiwei.crm.view.frament.menuFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.workbenchDao;
import com.example.yiwei.crm.dao_impl.workbenchDaoImpl;
import com.example.yiwei.crm.data.GetPersonnal;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.personnalEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/9.
 */

public class alreadyAppointmentFragment extends Fragment {
    private ExpandableListView listView;
    public String name[];
    public Integer[] state;
    public String []phone;

    MyExpandableListAdapter adapter;
    List<CustomerMessageEntity> work_list;
    List<String> list1,list3;
    List<Integer> list2;
    com.example.yiwei.crm.entity.personnalEntity personnalEntity;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootview=inflater.inflate(R.layout.alreadyappointment,container,false);
        listView= (ExpandableListView) rootview.findViewById(R.id.already_appointment_expandlist);
        personnalEntity= GetPersonnal.get();
        new Thread(new Runnable() {
            @Override
            public void run() {
                workbenchDao workbenchDao=new workbenchDaoImpl();
                work_list=workbenchDao.queryNeedContactMessage(baseClass.simple_alReady_appointment_url+personnalEntity.getPersonnal_id(),null);
                list1=new ArrayList<String>();
                list2=new ArrayList<Integer>();
                list3=new ArrayList<String>();
                for (int i=0;i<work_list.size();i++) {
                    list1.add(work_list.get(i).getClient_name());
                    list2.add(work_list.get(i).getFollw_state());
                    list3.add(work_list.get(i).getPhone_Number());
                }
                name=list1.toArray(new String[list1.size()]);
                state=list2.toArray(new Integer[list2.size()]);
                phone=list3.toArray(new String[list3.size()]);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listView.setGroupIndicator(null);
                        adapter = new MyExpandableListAdapter();
                        listView.setAdapter(adapter);
                        listView.setSelectedGroup(0);
                        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                            @Override
                            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                                Toast.makeText(getActivity(), "aa", Toast.LENGTH_SHORT).show();
                                return true;
                            }
                        });
                    }
                });
            }
        }).start();
        listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int i) {
                for (int i1=0;i1<name.length;i1++){
                    if (i!=i1){
                        listView.collapseGroup(i1);
                    }
                }
            }
        });
        return rootview;
    }
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return name.length;
        }
        @Override
        public int getChildrenCount(int i) {
            return 1;
        }
        @Override
        public Object getGroup(int i) {
            return name[i];
        }
        @Override
        public Object getChild(int i, int i1) {
            return null;
        }
        @Override
        public long getGroupId(int i) {
            return i;
        }
        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
        @Override
        public View getGroupView(  int i, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.expandable_super_item, null);
            }
            view.setTag(R.layout.expandable_super_item, i);
            view.setTag(R.layout.expandable_item_subclass, -1);
            TextView text_state = (TextView) view.findViewById(R.id.listview_frm_need_contact_txt_state);
            TextView text_name = (TextView) view.findViewById(R.id.listview_frm_need_contact_txt_name);
            TextView text_phone = (TextView) view.findViewById(R.id.listview_frm_need_contact_txt_phone);
            Button bt1 = (Button) view.findViewById(R.id.listview_add_item_img_zhankai);
            final int i1 =i;
            final boolean Tag =b;
            if (state[i].equals(0)){
                text_state.setText("未预约");
            }else if (state[i].equals(1)){
                text_state.setText("已预约");
            }else if (state[i].equals(2)){
                text_state.setText("已就诊");
            }

            text_name.setText(name[i]);
            text_phone.setText(phone[i]);
            if(b){
                bt1.setBackgroundResource(R.drawable.weizhankai);
            }else {
                bt1.setBackgroundResource(R.drawable.zhankai);
            }
            bt1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(!Tag){
                        listView.expandGroup(i1);

                    }else if(Tag){
                        listView.collapseGroup(i1);
                    }
                }
            });
            return view;
        }
        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.expandable_item_subclass, null);
            }
            view.setTag(R.layout.expandable_super_item, i);
            view.setTag(R.layout.expandable_item_subclass, i1);
            return view;
        }
        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }
    }
}
